<?php

namespace SLN0002\Controller;

use Home\Common\FIdConst;
use Home\Controller\PSIBaseController;
use Home\Service\UserService;
use SLN0002\Service\KaService;

/**
 * 记账Controller
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class KAController extends PSIBaseController
{
  /**
   * 明细分类账记账 - 主页面
   * 
   * 模板页面：web\Application\SLN0002\View\KA\keepDetailIndex.html
   */
  public function keepDetailIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
      $this->initVar();

      $this->assign("title", "明细分类账记账");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0002/KA/keepDetailIndex");
    }
  }

  /**
   * 明细分类账记账 - 主页面
   * 
   * 模板页面：web\Application\SLN0002\View\KA\keepGlIndex.html
   */
  public function keepGlIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::GL_KEEP_GL)) {
      $this->initVar();

      $this->assign("title", "总分类账记账");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0002/KA/keepGlIndex");
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function companyList()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_KEEP_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new KaService();
      $this->ajaxReturn($service->companyList(FIdConst::GL_KEEP_DETAIL));
    }
  }

  /**
   * 登记明细账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function keepDetail()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId")
      ];

      $ps = new KaService();
      $this->ajaxReturn($ps->keepDetail($params));
    }
  }

  /**
   * 查询明细账簿列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function queryAccList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"),
      ];

      $ps = new KaService();
      $this->ajaxReturn($ps->queryAccList($params));
    }
  }

  /**
   * 查询明细账的账样列
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function queryFmtColsForDetailAcc()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"), // 科目码
      ];

      $ps = new KaService();
      $this->ajaxReturn($ps->queryFmtColsForDetailAcc($params));
    }
  }

  /**
   * 查询明细账数据
   */
  public function queryDataForDetailAcc()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_KEEP_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "subAccLevel" => I("post.subAccLevel"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->queryDataForDetailAcc($params));
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function companyListForGl()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_KEEP_GL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new KaService();
      $this->ajaxReturn($service->companyList(FIdConst::GL_KEEP_GL));
    }
  }

  /**
   * 查询一级科目列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function subjectList()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"), // 模糊查询的科目码
      ];

      $service = new KaService();
      $this->ajaxReturn($service->subjectList($params));
    }
  }

  /**
   * 查询总分类账数据
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function queryAccGlData()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"),
        "year" => I("post.year"),
        "month" => I("post.month"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->queryAccGlData($params));
    }
  }

  /**
   * 登记总账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function keepGl()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->keepGl($params));
    }
  }

  /**
   * 关闭明细账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function closeAccDetail()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->closeAccDetail($params));
    }
  }

  /**
   * 取消关闭明细账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepDetailMainForm.js
   */
  public function cancelCloseAccDetail()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->cancelCloseAccDetail($params));
    }
  }

  /**
   * 关闭总账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function closeAccGL()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->closeAccGL($params));
    }
  }

  /**
   * 取消关闭总账
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\KA\KeepGlMainForm.js
   */
  public function cancelCloseAccGL()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_KEEP_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];

      $service = new KaService();
      $this->ajaxReturn($service->cancelCloseAccGL($params));
    }
  }
}
